//
//  ICDetailHeadView.m
//  ICome
//
//  Created by ENN on 16/4/21.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICDetailHeadView.h"
#import "ICDetailButton.h"
#import <SDWebImage/UIButton+WebCache.h>
#import "UIImage+AvatarGeneration.h"


#define ICHeadMaxCols 5
#define imageViewHeight  (App_Frame_Width - 80)

@interface ICDetailHeadView ()

@property (nonatomic, strong) UIButton *addBtn;

@property (nonatomic, strong) UIButton *deleteBtn;


@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UIButton *changeNameBtn;
@property (nonatomic, strong) UIView * bottomLine;

@end

@implementation ICDetailHeadView


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
    }
    return self;
}


#pragma mark - Event

- (void)deleteBtnClicked
{
    if ([self.headDelegate respondsToSelector:@selector(deleteBtnClicked)]) {
        [self.headDelegate deleteBtnClicked];
    }
}

- (void)addBtnClicked
{
    if ([self.headDelegate respondsToSelector:@selector(addBtnClicked)]) {
        [self.headDelegate addBtnClicked];
    }
}

- (void)headBtnClicked:(ICDetailButton *)headBtn
{
    if ([self.headDelegate respondsToSelector:@selector(headBtnClicked:User:)]) {
        [self.headDelegate headBtnClicked:headBtn.tag User:self.users[headBtn.tag]];
        //_headBtnBlock(_users[headBtn.tag]);
        
    }
}

- (void)changeName
{
    if ([self.headDelegate respondsToSelector:@selector(changeGroupName)]) {
        [self.headDelegate changeGroupName];
    }
}

- (void)changeHeadImg
{
    if ([self.headDelegate respondsToSelector:@selector(changeGroupHeadImg)]) {
        [self.headDelegate changeGroupHeadImg];
    }
}

#pragma mark - private

- (void)setUsers:(NSArray *)users
{
    _users    = users;
    
    NSUInteger count  = users.count;
    for (int i = 0; i < count; i ++) {
        ICUser *user = _users[i];
        ICDetailButton *headBtn = [[ICDetailButton alloc] init];
//        SingleAvatarBuilder
        [headBtn sd_setImageWithURL:[NSURL URLWithString:[ICTools getMaxImageURL:(user.photoId)]] forState:UIControlStateNormal placeholderImage:[UIImage SingleAvatarBuildereId:[user.eId characterAtIndex:user.eId.length-1] withName:user.eName withSize:CGSizeMake(160, 160) withPiont:CGPointMake(0, 0)] options:SDWebImageRetryFailed];
        NSLog(@"%@", user.photoId);
        [headBtn setTitle:user.eName forState:UIControlStateNormal];
         headBtn.tag             = i;
        [self addSubview:headBtn];
        [headBtn addTarget:self action:@selector(headBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    if(self.isPower){
    [self addSubview:self.addBtn];
    
    if (self.isMaster) {
        [self addSubview:self.deleteBtn];
        [self.topView addSubview:self.changeNameBtn];
    }
    }
    
    
    
    [self.topHeadBtn addTarget:self action:@selector(changeHeadImg) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)setGroup:(ICGroup *)group
{
    _group = group;
    
    NSURL *url = [NSURL URLWithString:[ICTools getMaxImageURL:(group.photoId)]];
    
    //[self.topHeadBtn sd_setBackgroundImageWithURL:url forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"App_group_headimg"]];
//    [self.topHeadBtn sd_setImageWithURL:url forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"App_group_headimg"]];
    [self.topHeadBtn sd_setImageWithURL:url forState:UIControlStateNormal placeholderImage:[UIImage GroupHeadgId:group.gId withSize:CGSizeMake(160, 160)] options:SDWebImageRetryFailed];
    NSString *groupName = [ICMessageDatabase getGroupName:group.gId];
    if (groupName.length > 13) {
       groupName = [groupName stringByReplacingOccurrencesOfString:[groupName substringWithRange:NSMakeRange(13, groupName.length - 13)] withString:@"..."];
    }
    self.nameLabel.text = groupName;
    if (![group.gType isEqualToString:@"2"] && ![group.gType isEqualToString:@"11"]) {
        [_topView removeFromSuperview];
    } else {
//        [self addSubview:self.topView];
    }
}

- (void)modifyHeadImageWithPhotoId:(NSString *)photoId  withGroup:(ICGroup *)group{
    NSURL *url = [NSURL URLWithString:[ICTools getMaxImageURL:(photoId)]];
    //[self.topHeadBtn sd_setBackgroundImageWithURL:url forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"App_group_headimg"]];
//     [self.topHeadBtn sd_setImageWithURL:url forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"App_group_headimg"]];
    [self.topHeadBtn sd_setImageWithURL:url forState:UIControlStateNormal placeholderImage:[UIImage GroupHeadgId:group.gId withSize:CGSizeMake(160, 160)] options:SDWebImageRetryFailed];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat topImageH = ICTopImageH;
    if (![_group.gType isEqualToString:@"2"] && ![_group.gType isEqualToString:@"11"]) {
        topImageH = 0;
    }
    self.topView.frame = CGRectMake(0, -topImageH, self.width, topImageH);
   
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.topView.mas_centerX);
        make.bottom.equalTo(self.topView.mas_bottom).offset(-5);
        make.height.mas_equalTo(40);
    }];
    [self.topHeadBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.topView.mas_centerX);
        make.bottom.equalTo(self.nameLabel.mas_top).offset(-5);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(80);
    }];
   
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView.mas_bottom).offset(-0.5);
        make.left.equalTo(self.topView.mas_left).offset(15);
        make.right.equalTo(self.topView.mas_right).offset(-29.5);
        make.height.mas_equalTo(0.5);
    }];
    
    CGFloat inset            = 14;
    NSUInteger count = self.users.count;
    CGFloat btnW     = (self.width - 2*inset)/ICHeadMaxCols;
    CGFloat btnH     = btnW+5;
    if (iPhone5) {
        btnH = btnH+20;
    }
    if (iPhone6) {
        btnH = btnH+8;
    }
    if ([ICTools isIPhoneX]) {
        btnH = btnH+8;
    }
    for (int i = 0; i < count; i ++) {
        ICDetailButton *btn = self.subviews[i];
        btn.width           = btnW;
        btn.height          = btnH;
        btn.x               = inset + (i % ICHeadMaxCols)*btnW;
        btn.y               = (i / ICHeadMaxCols)*btnH + 10;
    }
    if(self.isPower){
        
        if (self.isMaster) {
            [self.changeNameBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(self.nameLabel.mas_centerY);
                make.left.equalTo(self.nameLabel.mas_right).offset(-5);
                make.width.mas_equalTo(40);
                make.height.mas_equalTo(40);
            }];
        }
        
    self.addBtn.width     = btnW;
    self.addBtn.height    = btnH;
    self.addBtn.x         = inset + (count%ICHeadMaxCols)*btnW;
    self.addBtn.y         = (count/ICHeadMaxCols)*btnH + 10;
    self.addBtn.imageEdgeInsets =  UIEdgeInsetsMake((btnW-37)/2.0-10,0,(btnW-37)/2.0+10,0);
    
    if (self.isMaster && _users.count >= 2) { // 3个人以上
        self.deleteBtn.width     = btnW;
        self.deleteBtn.height    = btnH;
        self.deleteBtn.x         = inset + ((count+1)%ICHeadMaxCols)*btnW;
        self.deleteBtn.y         = ((count+1)/ICHeadMaxCols)*btnH + 10;
        self.deleteBtn.imageEdgeInsets =  UIEdgeInsetsMake((btnW-37)/2.0-10,0,(btnW-37)/2.0+10,0);
    }
    }
}


- (UIButton *)addBtn
{
    if (_addBtn == nil) {
        _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addBtn setImage:[UIImage imageNamedFromiComeSDKBundle:@"icon_chat_add"] forState:UIControlStateNormal];
        [_addBtn addTarget:self action:@selector(addBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _addBtn;
}

- (UIButton *)deleteBtn
{
    if (!_deleteBtn) {
        _deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_deleteBtn setImage:[UIImage imageNamedFromiComeSDKBundle:@"icon_chat_delete"] forState:UIControlStateNormal];
        [_deleteBtn addTarget:self action:@selector(deleteBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteBtn;
}

- (UIView *)topView
{
    if (!_topView) {
        _topView = [[UIView alloc] init];
        _topView.backgroundColor = [UIColor whiteColor];//ICRGB(0x00a2d5);//[UIColor whiteColor];//ICRGB(0x00a2d5);
        [_topView addSubview:self.topHeadBtn];
        [_topView addSubview:self.nameLabel];
        [_topView addSubview:self.bottomLine];
    }
    return _topView;
}

-(UIView *)bottomLine
{
    if(!_bottomLine)
    {
        _bottomLine = [[UIView alloc]init];
        _bottomLine.backgroundColor = LINEBACKGROUNDCOLOR;
    }
    return _bottomLine;
}

- (UILabel *)nameLabel
{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.textColor = ICRGB(0x0a0a0a);
        _nameLabel.font = ICFont(16 * Level);
        _nameLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _nameLabel;
}

- (UIButton *)topHeadBtn
{
    if (!_topHeadBtn) {
        _topHeadBtn = [ICLCBFullScreenButton buttonWithType:UIButtonTypeCustom];
        _topHeadBtn.layer.masksToBounds = YES;
        _topHeadBtn.backgroundColor = ICRGB(0xe0e0e0);
        _topHeadBtn.layer.cornerRadius  = 5;
    }
    return _topHeadBtn;
}



- (UIButton *)changeNameBtn
{
    if (!_changeNameBtn) {
        _changeNameBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_changeNameBtn setImage:[UIImage imageNamedFromiComeSDKBundle:@"icon_modify_groupName"] forState:UIControlStateNormal];
        [_changeNameBtn addTarget:self action:@selector(changeName) forControlEvents:UIControlEventTouchUpInside];
    }
    return _changeNameBtn;
}


@end
